/**
 * 日报活-分部数据
 * wangmei
 */
import React, { Component } from 'react';
import { Form, message  } from 'antd';
import { getLogInstallDailyChartWithIP } from '../../../Server/SystemManger';
import CommonDayPart from './CommonDayPart';
import moment from 'moment';
import qs from 'qs';

import {REQUEST_SUCCESS,REQUEST_UNAUTHORIZED} from '../../../common/const';
require('../../systemManager/customer.css');

const dateFormat = 'YYYY-MM-DD';
const monthFormat = 'YYYY-MM';
const currentDate = new Date();

class DayPart extends Component{
    constructor(props) {
        super(props);
        const { sysCompany } = this.props;
        this.state = {
            data: [], //分部数据表格数据
            loading: false, // 分部数据加载状态
            xData: [], //分部数据x轴数据
            yData: [], //分部数据y轴数据
            partNameData: [], //分部名数据
            startIp: sysCompany.startIp, //分部管理起始ip
            endIp: sysCompany.endIp, //分部管理结束ip
            titleDate: currentDate  //标题日期显示
        };
    }

    //查询分部方法
    getMonthChart = (values, startValue, endValue) =>{
        this.setState({ loading: true });
        const param = {
            queryMonth: moment(values.queryMonth).format(monthFormat),
            startIp: startValue,
            endIp: endValue,
            sn: values.monthSn,
            version: values.monthVersion,
            distNum: values.monthDistNum
        };
        const _param = qs.stringify(param);
        getLogInstallDailyChartWithIP(_param).then(res => {
            if(res && res.code == REQUEST_SUCCESS){
                let data = res.data.points;
                let xData = [];
                let yData = [];
                for(let i = 0; i < data.length; i++){
                    xData.push(data[i].x.substr(data[i].x.length-2,2)+'日');
                    yData.push(data[i].y);
                }
                this.setState({
                    data: data,
                    xData: xData,
                    yData: yData,
                    loading: false,
                    titleDate: values.queryMonth
                })
            }else {
                this.setState({
                    loading: false
                })
                res && res.code !== REQUEST_UNAUTHORIZED && message.error(res.msg);
            }
        });
    }

    render(){           
        const { sysCompany, state } = this.props;
        const { data, loading, xData, yData, titleDate } = this.state;
        let _data = [];
        for(var i=0;i<data.length;i++){
            let temp = {
                "日期": data[i].x,
                "日安装量": data[i].y,
            }
            _data.push(temp);
        }

        const columns = [
            {
                title: "日期",
                dataIndex: "x",
                align: 'center',
                width: '50%',
                render: (text, record)=>{
                    return moment(text).format(dateFormat);
                }
            }, {
                title: "日安装量",
                dataIndex: "y",
                align: 'center',
                width: '50%',
            }
        ];

        return(
            <CommonDayPart
                sysCompany={sysCompany}
                state={state}
                columns={columns}
                data={data}
                loading={loading}
                xData={xData}
                yData={yData}
                xName="日期" 
                yName="日安装量"  
                title={"日安装量信息（"+moment(titleDate).format('YYYY年MM月')+"）"}
                legendData={['日安装量信息']}
                formatter={moment(titleDate).format('YYYY年MM月')+'{b}<br />{a} : {c}'}
                _headers={['日期', '日安装量' ]}
                _data={_data}
                fileName={'日安装量信息'}
                getChartData={this.getMonthChart}
            />
        )
    }
}

export default Form.create()(DayPart)